//
//  SearchRentHouseVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/15.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "SearchRentHouseVC.h"
#import "RentHouseNavView.h"
#import "RenHouseTopImageCell.h"
#import "RenHouseHomeListCell.h"
#import "RentHouseDetailVC.h"
#import "HouseListParameter.h"
#import "HousRentAdvsModel.h"
#import "HouseParameterModel.h"
#import "HouseParameSub.h"
#import "ProvinceModel.h"
#import "CitysModel.h"
#import "DistrictsModel.h"
#import "RentHouseNothingCell.h"
@interface SearchRentHouseVC ()

@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, assign) NSString *pageCount;

@property (nonatomic, assign) NSString *totalCount;

@property (nonatomic, strong) NSMutableArray<HouseParameterModel *> *dataModels;

@property (nonatomic, strong) HouseListParameter *dataParameters;

/** <#note#> */
@property (nonatomic, assign) CGFloat topViewH;



@end



@implementation SearchRentHouseVC

- (HouseListParameter *)dataParameters{
    
    if (!_dataParameters) {
        
        _dataParameters = [[HouseListParameter alloc]init];
        
    }
    
    return _dataParameters;
}









- (void)popCurrentController{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    self.groupTableView.tableFooterView = [[UIView alloc]init];
    self.groupTableView.separatorInset = UIEdgeInsetsZero;
    self.groupTableView.backgroundColor = [UIColor whiteColor];
    
    [self.groupTableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:[BaseTableViewCell identifierStr]];
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([RenHouseHomeListCell class]) bundle:nil] forCellReuseIdentifier:[RenHouseHomeListCell identifierStr]];
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([RentHouseNothingCell class]) bundle:nil] forCellReuseIdentifier:[RentHouseNothingCell identifierStr]];
    
    
//
//    //    CGFloat navH = IPhoneX ? 44 : 20;
//    CGFloat tabbarH = IPhoneX ? 132 : 108;
//
//    if (@available(iOS 11.0, *)) {
//        //        contentInsetAdjustmentBehavior属性也是用来取代automaticallyAdjustsScrollViewInsets属性的，推荐使用这种方式。
//        self.groupTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//
//        self.groupTableView.contentInset = UIEdgeInsetsMake(tabbarH, 0, 0, 0);
//
//    }else{
//
//        self.groupTableView.contentInset = UIEdgeInsetsMake(tabbarH, 0, 0, 0);
//
//    }
    
    
    
    
    
    MJWeakSelf
    self.groupTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.groupTableView.mj_header beginRefreshing];
    
    
    //    self.groupTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
    //        [weakSelf loadMoreData];
    //    }];
    
    
    
    
}


/**
 * 房源列表接口
 * page                 当前第几页 （默认1）
 * pagesize             每页数据总条数（默认10）
 * languageId           语种id
 * provinceId           所在区域一级地区
 * cityId               所在区域二级地区
 * payClassId           付款方式id
 * rentingStyleId       租房方式id
 
 */

- (void)loadData{
    
    
    self.curpage = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"page"] = @"1";
    parameters[@"pagesize"] = @"20";
    parameters[@"keyword"] = self.keyword;

   
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"house/lists" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            //            MYJSONSTR
            //            _model.lastString
            
            //           [[responseObject[@"data"][@"house"] firstObject]  propertyCode];
            
            weakSelf.pageCount = responseObject[@"data"][@"page_count"];
            
            weakSelf.totalCount = responseObject[@"data"][@"total_count"];
            
            
            
            //            [weakSelf.ADVSModels removeAllObjects];
            weakSelf.dataModels = [HouseParameterModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"house"]];
            
            if (myViewModel.parameterModel != nil) {
                for (HouseParameterModel *model in weakSelf.dataModels) {
                    
                    model.language = myViewModel.parameterModel.language;
                    model.huxing = myViewModel.parameterModel.huxing;
                    model.fukuan = myViewModel.parameterModel.fukuan;
                    model.rentingStyle = myViewModel.parameterModel.rentingStyle;
                    model.decorateStyle = myViewModel.parameterModel.decorateStyle;
                    model.houseStyle = myViewModel.parameterModel.houseStyle;
                    model.position = myViewModel.parameterModel.position;
                    
                    [weakSelf setupValues:model];
                }
            }
            
            
            [weakSelf.groupTableView.mj_footer resetNoMoreData];
            
            //            weakSelf.menu.hidden = NO;
            
            
            if ([weakSelf.totalCount isEqualToString:@"0"]) {
                
                weakSelf.groupTableView.mj_footer.hidden = YES;
                
                weakSelf.groupTableView.mj_footer.height = 0;
                
                weakSelf.groupTableView.mj_footer = nil;
                
                [weakSelf.groupTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                
                
                
                weakSelf.groupTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
                    
                    [weakSelf loadMoreData];
                    
                }];
                
                [weakSelf.groupTableView.mj_footer resetNoMoreData];
                
                
                if ([weakSelf.pageCount isEqualToString:@"1"]) {
                    
                    [weakSelf.groupTableView.mj_footer endRefreshingWithNoMoreData];
                    
                }
                
                
            }
            
            
    
            
            
            [weakSelf.groupTableView.mj_header endRefreshing];
            
            [weakSelf.groupTableView reloadData];
            
            
            
            
            
        } else {
            
            [weakSelf.groupTableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
    }];
    
    
    
    
    
    
    
    
    
    
}






- (void)loadMoreData{
    
    
    
    self.curpage ++;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pagesize"] = @"20";
    parameters[@"page"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    parameters[@"keyword"] = self.keyword;
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"house/lists" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            //            MYJSONSTR;
            
            //            weakSelf.ADVSModels = [HousRentAdvsModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"advs"]];
            
            
            
            NSMutableArray<HouseParameterModel *> *temp = [HouseParameterModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"house"]];
            
            
            
            if (myViewModel.parameterModel != nil) {
                
                for (HouseParameterModel *model in temp) {
                    
                    model.language = myViewModel.parameterModel.language;
                    model.huxing = myViewModel.parameterModel.huxing;
                    model.fukuan = myViewModel.parameterModel.fukuan;
                    model.rentingStyle = myViewModel.parameterModel.rentingStyle;
                    model.decorateStyle = myViewModel.parameterModel.decorateStyle;
                    model.houseStyle = myViewModel.parameterModel.houseStyle;
                    model.position = myViewModel.parameterModel.position;
                    
                    [weakSelf setupValues:model];
                    
                }
            }
            
            
            [weakSelf.dataModels addObjectsFromArray:temp];
            
            
            
            
            
            
            
            if (weakSelf.pageCount.integerValue == self.curpage) {
                
                [weakSelf.groupTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [weakSelf.groupTableView.mj_footer endRefreshing];
                
                
            }
            
            
            [weakSelf.groupTableView reloadData];
            
        }else{
            
            [weakSelf.groupTableView.mj_footer endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
    }];
    
    
    
    
    
}




- (void)setupValues:(HouseParameterModel *)model{
    
    
    
    for (HouseParameSub *sub1 in model.position) {
        if ([sub1.class_id isEqualToString:model.house_face]) {
            model.house_face_name = sub1.name;
        }
    }
    
    
    
    
    //    for (HouseParameSub *sub2 in model.language) {
    //        if ([sub2.class_id isEqualToString:model.house_language]) {
    //            model.house_language_name = sub2.name;
    //        }
    //    }
    
    
    for (HouseParameSub *sub2 in model.decorateStyle) {
        if ([sub2.class_id isEqualToString:model.house_decorate]) {
            model.house_decorate_name = sub2.name;
        }
    }
    
    
    
    
    
    
    
    
    
}







//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//
////    [self.navigationController setNavigationBarHidden:YES animated:YES];
//
//
//}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataModels == nil) {
        return 0;
    } else {
        return 1;
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (self.dataModels == nil) {
        return 0;
    } else {
        
        if (self.dataModels.count == 0) {
            
            return 1;
            
        } else {
            
            return self.dataModels.count;
            
        }
    }
    
    
    
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataModels.count == 0) {
        RentHouseNothingCell *cell = [tableView dequeueReusableCellWithIdentifier:[ RentHouseNothingCell identifierStr]];
        
        
        return cell;
        
    } else {
        RenHouseHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:[RenHouseHomeListCell identifierStr]];
        
        cell.model = self.dataModels[indexPath.row];
        
        return cell;
    }
    
    
    
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if (self.dataModels.count == 0) {
        
        
        return  KScreenH - (IPhoneX ? 88 : 64);
        
    } else {
        
        return 124;
    }
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 0.00001;
    
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [[UIView alloc]init];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc]init];
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    RentHouseDetailVC *temp = [[RentHouseDetailVC alloc]init];
    
    temp.houseId = self.dataModels[indexPath.row].house_id;
    
    
    [self.navigationController pushViewController:temp animated:YES];
    
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}







@end
